package com.ruoyi.quartz.task;
/*
 * @ClassName PointsTask
 * @Description //TODO
 * @Author Administrator
 * @Date 2024/4/3 15:20
 **/

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AssesRule;
import com.ruoyi.system.domain.AssesRuleSystem;
import com.ruoyi.system.domain.PointsRule;
import com.ruoyi.system.domain.PointsRuleSystem;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class AssesTask {

    @Autowired
    private IAssesRuleService assesRuleService;
    @Autowired
    private IAssesRuleSystemService assesRuleSystemService;
    @Autowired
    private IAssesManagementService assesManagementService;


    /**
     * 修改考核规则状态
     */
    @Scheduled(cron = "0 0 2 1 1 ?")
    public void updateAssesRuleStatusTask(){
        //去年规则过期
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        String lastYear = DateUtils.getLastYear();
        assesRule.setEffectiveTerm(lastYear);
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);
        for(AssesRule rule : assesRules){
            AssesRule assesRule1Vo = new AssesRule();
            assesRule1Vo.setId(rule.getId());
            assesRule1Vo.setStatus("1");
            assesRuleService.updateAssesRule(assesRule1Vo);
        }
        //如果有今年的规则修改为正常状态
        String nowYear = DateUtils.getNowYear();
        assesRule.setEffectiveTerm(nowYear);
        AssesRule assesRuleVo = new AssesRule();
        assesRuleVo.setEffectiveTerm(nowYear);
        List<AssesRule> assesRulesList = assesRuleService.selectAssesRuleList(assesRuleVo);
        for(AssesRule rule : assesRulesList){
            rule.setStatus("0");
            assesRuleService.updateAssesRule(rule);
        }
    }


    /**
     * 执行考核系统规则（周）
     */
    @Scheduled(cron = "0 1 0 ? * 2")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void updateSystemAssesByWeek(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);
        for(AssesRule rule : assesRules){
            //查询出按周执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("0");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //上周一
            Date monday = DateUtils.getMonday();
            String mondayString = DateUtil.format(monday, "yyyy-MM-dd");
            //上周天
            DateTime dateTime = DateUtil.offsetDay(monday, 6);
            String sundayString = DateUtil.format(dateTime, "yyyy-MM-dd");
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,mondayString,sundayString,deptType);
        }
    }

    /**
     * 执行系统规则（月）
     */
    @Scheduled(cron = "0 1 0 1 * ?")
    public void updateSystemPointsByMonth(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);
        for(AssesRule rule : assesRules){
            //查询出按月执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("1");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //上月第一天
            Date firstDay = DateUtils.getLastMonthFirtDay();
            String firstDayString = DateUtil.format(firstDay, "yyyy-MM-dd");
            //上月最后一天
            Date lastDay = DateUtils.getLastMonthLastDay();
            String lastDayString = DateUtil.format(lastDay, "yyyy-MM-dd");
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,firstDayString,lastDayString,deptType);
        }

    }

    /**
     * 执行系统规则（季）每个季度的第一个月的一号的00:01:00 执行一次
     */
    @Scheduled(cron = "0 1 0 1 1,4,7,10 ?")
    public void updateSystemPointsByQuarter(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);

        for(AssesRule rule : assesRules){
            //查询出按季度执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("2");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //上个季度第一天
            Date firstDay = DateUtils.getLastQuarterFirstDay();
            String firstDayString = DateUtil.format(firstDay, "yyyy-MM-dd");
            //上个季度最后一天
            Date lastDay = DateUtils.getLastQuarterLastDay();
            String lastDayString = DateUtil.format(lastDay, "yyyy-MM-dd");
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,firstDayString,lastDayString,deptType);
        }
    }

    /**
     * 执行系统规则（半年）
     */
    @Scheduled(cron = "0 1 0 1 7 ?")
    public void updateSystemPointsByHalfYear1(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);

        for(AssesRule rule : assesRules){
            //查询出按半年执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("3");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //1月1日
            String yearFirstDay = DateUtils.getYearFirstDay();
            //6月30日
            String year = yearFirstDay.substring(0, 4);
            String lastDayString = year+"-06-30";
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,yearFirstDay,lastDayString,deptType);
        }
    }

    /**
     * 执行系统规则（半年）
     */
    @Scheduled(cron = "0 1 0 1 1 ?")
    public void updateSystemPointsByHalfYear2(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);

        for(AssesRule rule : assesRules){
            //查询出按半年执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("3");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //去年7月1号
            String lastYear = DateUtils.getLastYear();
            String firstDayString = lastYear+"-07-01";
            //去年12月31号
            String lastDayString = lastYear+"-12-31";
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,firstDayString,lastDayString,deptType);
        }
    }

    /**
     * 执行系统规则（年）
     */
    @Scheduled(cron = "0 1 0 1 1 ?")
    public void updateSystemPointsByYear(){
        //查询出正常的规则
        AssesRule assesRule = new AssesRule();
        assesRule.setStatus("0");
        List<AssesRule> assesRules = assesRuleService.selectAssesRuleList(assesRule);
        for(AssesRule rule : assesRules){
            //查询出按年执行的系统规则
            AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
            assesRuleSystem.setCyclicalPattern("4");
            assesRuleSystem.setDelFlag("1");
            assesRuleSystem.setAssesRuleId(rule.getId());
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
            //去年1月1号
            String lastYear = DateUtils.getLastYear();
            String firstDayString = lastYear+"-01-01";
            //去年12月31号
            String lastDayString = lastYear+"-12-31";
            String deptType = rule.getDeptType();
            assesManagementService.insertAssesManagementSystem(assesRuleSystems,firstDayString,lastDayString,deptType);
        }
    }
}
